﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace YArchitech.Revit.Electric.Common
{
	public class FamilyLibDataAssist
	{
		public FamilyLibDataAssist(Document document, string tableName)
		{
			this.doc = document;
			this.dbTableName = tableName;
			this.familyConfig = new FamilyConfig(tableName);
		}

		public List<DbFamilyObject> LoadFamilyDatas(IFilterFamilyObject filter = null)
		{
			List<DbFamilyObject> list = new List<DbFamilyObject>();
			if (string.IsNullOrEmpty(this.dbTableName))
			{
				return list;
			}
			this.familyConfig.GetParameterConfig();
			SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
			foreach (ListItem listItem in spaceFamilyDataReader.GetAllName(this.dbTableName))
			{
				int id = Convert.ToInt32(listItem.Tag);
				DbFamilyObject dbFamilyObject = new DbFamilyObject();
				foreach (ListItem item in spaceFamilyDataReader.GetInfoById(this.dbTableName, id, this.doc))
				{
					this.SetValueForFamilyObject(dbFamilyObject, item);
				}
				if (filter == null || filter.IsMatch(dbFamilyObject))
				{
					list.Add(dbFamilyObject);
				}
			}
			return list;
		}

		public bool SetFamilyDatas(DbFamilyObject familyObject)
		{
			if (familyObject == null)
			{
				return false;
			}
			List<ListItem> list = new List<ListItem>();
			foreach (DbFamilyParameter dbFamilyParameter in familyObject.Parameters)
			{
				if (!dbFamilyParameter.ReadOnly)
				{
					ListItem item = new ListItem(dbFamilyParameter.Name, dbFamilyParameter.Value.ToString());
					list.Add(item);
				}
			}
			return new SpaceFamilyDataReader().SetInfoById(this.dbTableName, familyObject.Id, list);
		}

		private void SetValueForFamilyObject(DbFamilyObject familyObject, ListItem item)
		{
			if (familyObject == null || item == null)
			{
				return;
			}
			if (item.Name == "Id")
			{
				familyObject.Id = Convert.ToInt32(item.Value);
				return;
			}
			if (item.Name == "Name")
			{
				familyObject.Name = item.Value;
				return;
			}
			if (item.Name == "Family")
			{
				familyObject.Family = item.Value;
				return;
			}
			if (item.Name == "Image")
			{
				familyObject.Image = item.Value;
				return;
			}
			if (item.Name == "Image2D")
			{
				familyObject.Image2D = item.Value;
				return;
			}
			if (item.Name == "Legend")
			{
				familyObject.Legend = item.Value;
				return;
			}
			if (item.Name == "Version")
			{
				return;
			}
			DbFamilyParameter dbFamilyParameter = new DbFamilyParameter();
			dbFamilyParameter.Name = item.Name;
			dbFamilyParameter.Value = item.Value;
			dbFamilyParameter.Tag = item.Tag;
			familyObject.Parameters.Add(dbFamilyParameter);
			DbParameter dbParameter = this.familyConfig.Parameters.Find((DbParameter p) => item.Name == p.Name);
			if (dbParameter != null)
			{
				dbFamilyParameter.SharedName = dbParameter.SharedName;
				dbFamilyParameter.Unit = dbParameter.Unit;
				dbFamilyParameter.ReadOnly = dbParameter.ReadOnly;
			}
		}

		private Document doc;

		private string dbTableName = string.Empty;

		private FamilyConfig familyConfig;
	}
}
